package cn.itcast.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TTLMessageConfig {

//    @Bean
//    public DirectExchange ttlDirectExchange() {
//        return new DirectExchange("ttl.direct");
//    }
//
//    @Bean
//    public Queue ttlQueue() {
//        return QueueBuilder
//                .durable("ttl.queue")
//                .ttl(10000)
//                .deadLetterExchange("dl.direct")
//                .deadLetterRoutingKey("dl")
//                .build();
//    }
//
//    @Bean
//    public Binding ttlBinding() {
//        return BindingBuilder.bind(ttlQueue()).to(ttlDirectExchange()).with("ttl");
//    }

    @Bean
    public DirectExchange ttlDirectExchange() {
        return new DirectExchange("ttl.direct");
    }

    @Bean
    public Queue ttlQueue() {
        return QueueBuilder
                .durable("ttl.queue")
                .ttl(10000)  //延时 10 s
                .deadLetterExchange("dl.direct") //消息如果超时没被消费就给这个死信交换机
                .deadLetterRoutingKey("dl")
                .build();
    }

    @Bean
    public Binding ttlBinding() {
        return BindingBuilder.bind(ttlQueue()).to(ttlDirectExchange()).with("ttl");
    }

}
